package com.live.activity;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.live.adapter.LiveMatchSimpleAdapter;
import com.live.common.Constant;
import com.live.task.LiveMatch;
import com.live.task.LiveUpgrade;
import com.live.tools.LiveTools;

public class LiveMatchActivity extends LiveBaseActivity {

	private String today;

	private String matchCategory = "0";

	private ListView matchList = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_match);

		TextView matchDate = (TextView) findViewById(R.id.matchDate);
		today = sdf.format(System.currentTimeMillis());
		matchDate.setText(today);

		matchList = (ListView) findViewById(R.id.matchList);

		LiveMatchSimpleAdapter adapter = new LiveMatchSimpleAdapter(context, liveMatchList, R.layout.match_list,
				new String[] { "matchName", "matchTime", "homeTeam", "homeTeamScore", "visitTeam", "visitTeamScore",
						"matchStatus", "matchRecord", "matchWatch" }, new int[] { R.id.matchName, R.id.matchTime,
						R.id.homeTeam, R.id.homeTeamScore, R.id.visitTeam, R.id.visitTeamScore, R.id.matchStatus,
						R.id.matchRecord, R.id.matchWatch }, today);

		matchList.setAdapter(adapter);

		// 赛程列表短按监听事件
		matchList.setOnItemClickListener(new OnItemClickListener() {

			// 从当前画面到比分明细画面跳转，并传值。
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				Map<String, Object> sportsMatch = liveMatchList.get(arg2);

				Intent intent = new Intent();

				intent.setClass(context, LiveDetailActivity.class);

				// 实例化 Bundle，设置需要传递的参数
				Bundle bundle = new Bundle();
				bundle.putString("matchId", sportsMatch.get("matchId").toString());
				bundle.putString("matchOptaId", sportsMatch.get("matchOptaId").toString());
				bundle.putString("matchName", sportsMatch.get("matchName").toString());
				if (sportsMatch.get("matchRecord") != null) {
					bundle.putString("matchRecord", sportsMatch.get("matchRecord").toString());
				}
				bundle.putString("matchCategory", sportsMatch.get("matchCategory").toString());
				bundle.putString("matchTime", today + " " + sportsMatch.get("matchTime").toString());
				bundle.putString("homeTeam", sportsMatch.get("homeTeam").toString());
				bundle.putString("homeTeamName", sportsMatch.get("homeTeamName").toString());
				bundle.putString("homeTeamScore", sportsMatch.get("homeTeamScore").toString());
				bundle.putString("homeTeamPic", sportsMatch.get("homeTeamPic").toString());
				bundle.putString("visitTeam", sportsMatch.get("visitTeam").toString());
				bundle.putString("visitTeamName", sportsMatch.get("visitTeamName").toString());
				bundle.putString("visitTeamScore", sportsMatch.get("visitTeamScore").toString());
				bundle.putString("visitTeamPic", sportsMatch.get("visitTeamPic").toString());
				bundle.putString("matchStatus", sportsMatch.get("matchStatus").toString());
				bundle.putString("matchWatch", sportsMatch.get("matchWatch").toString());

				intent.putExtras(bundle);

				context.startActivity(intent);
			}
		});

		// 赛程列表长按监听事件
		matchList.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				matchList.showContextMenu();
				return true;
			}

		});

		// 添加赛程列表长按菜单
		/*
		 * matchList.setOnCreateContextMenuListener(new
		 * OnCreateContextMenuListener() {
		 * 
		 * @Override public void onCreateContextMenu(ContextMenu menu, View v,
		 * ContextMenuInfo info) { // menu.setHeaderTitle(R.string.contentMenu);
		 * menu.add("StopService"); menu.add("BackGround");
		 * menu.add("RequireData"); menu.add("StartService");
		 * menu.add("RequireDataDelay 5s"); menu.add("RequireDataRepeating 5s");
		 * }
		 * 
		 * });
		 */

		// 单击日期
		matchDate.setOnClickListener(new TextView.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		// 长按日期
		matchDate.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				today = sdf.format(new Date(System.currentTimeMillis()));
				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);
				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
				return true;
			}
		});

		TextView searchBtn = (TextView) findViewById(R.id.searchBtn);
		searchBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();

				intent.setClass(context, LiveSearchActivity.class);

				context.startActivity(intent);
			}
		});

		ImageView forwardDayBtn = (ImageView) findViewById(R.id.forwardDayBtn);
		forwardDayBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					Date date = sdf.parse(today);
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					cal.add(5, -1);
					today = sdf.format(cal.getTime());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);
				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		ImageView nextDayBtn = (ImageView) findViewById(R.id.nextDayBtn);
		nextDayBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					Date date = sdf.parse(today);
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					cal.add(5, 1);
					today = sdf.format(cal.getTime());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);
				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		// 全部比赛
		TextView matchAll = (TextView) findViewById(R.id.matchAll);
		matchAll.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTab();

				findViewById(R.id.matchAllLine).setVisibility(Constant.VISIBLE);

				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);

				matchCategory = "0";

				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		// 篮球比赛
		TextView matchBtb = (TextView) findViewById(R.id.matchBtb);
		matchBtb.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTab();
				findViewById(R.id.matchBtbLine).setVisibility(Constant.VISIBLE);

				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);

				matchCategory = "1";

				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		// 足球比赛
		TextView matchFtb = (TextView) findViewById(R.id.matchFtb);
		matchFtb.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTab();
				findViewById(R.id.matchFtbLine).setVisibility(Constant.VISIBLE);

				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);

				matchCategory = "2,3";

				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		// 台球比赛
		TextView matchTbb = (TextView) findViewById(R.id.matchTbb);
		matchTbb.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTab();
				findViewById(R.id.matchTbbLine).setVisibility(Constant.VISIBLE);

				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);

				matchCategory = "5";

				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		// 网球比赛
		TextView matchTsb = (TextView) findViewById(R.id.matchTsb);
		matchTsb.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTab();
				findViewById(R.id.matchTsbLine).setVisibility(Constant.VISIBLE);

				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);

				matchCategory = "6";

				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		// 刷新比赛别表
		TextView refreshBtn = (TextView) findViewById(R.id.refreshBtn);
		refreshBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage()
						+ "&matchTime=" + today + "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		// 用户设置
		TextView userSetting = (TextView) findViewById(R.id.userSetting);
		userSetting.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();

				intent.setClass(context, LiveUserSettingActivity.class);

				context.startActivity(intent);
			}
		});

		// 升级检测
		LiveUpgrade liveUpgradeTask = new LiveUpgrade(context);
		liveUpgradeTask.execute();

	}

	// 长按菜单响应函数
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		return super.onContextItemSelected(item);
	}

	private void changeTab() {
		TextView matchAllLine = (TextView) findViewById(R.id.matchAllLine);
		matchAllLine.setVisibility(Constant.INVISIBLE);
		TextView matchBtbLine = (TextView) findViewById(R.id.matchBtbLine);
		matchBtbLine.setVisibility(Constant.INVISIBLE);
		TextView matchFtbLine = (TextView) findViewById(R.id.matchFtbLine);
		matchFtbLine.setVisibility(Constant.INVISIBLE);
		TextView matchTsbLine = (TextView) findViewById(R.id.matchTsbLine);
		matchTsbLine.setVisibility(Constant.INVISIBLE);
		TextView matchTbbLine = (TextView) findViewById(R.id.matchTbbLine);
		matchTbbLine.setVisibility(Constant.INVISIBLE);
	}

	// 设置要检索的日期
	private DatePickerDialog.OnDateSetListener mSetDateListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int month, int day) {
			Calendar cal = Calendar.getInstance();
			cal.set(year, month, day);
			today = sdf.format(cal.getTime());

			TextView matchDate = (TextView) findViewById(R.id.matchDate);
			matchDate.setText(today);
			String url = Constant.SITE_URL + "live_match.php?locale=" + LiveTools.getLocaleLanguage() + "&matchTime="
					+ today + "&matchCategory=" + matchCategory;

			LiveMatch task = new LiveMatch(context, matchList, today);
			task.execute(url);
		}
	};

	protected Dialog onCreateDialog(int id) {
		Calendar c = Calendar.getInstance();
		switch (id) {
		case 0:
			return new DatePickerDialog(this, this.mSetDateListener, c.get(1), c.get(2), c.get(5));
		}
		return null;
	}

	public void onConfigurationChanged(Configuration config) {
		super.onConfigurationChanged(config);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(Constant.EXIT_INFO).setCancelable(false)
					.setPositiveButton("是", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							userExit();
						}
					}).setNegativeButton("否", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
			builder.show();
			return super.onKeyDown(keyCode, event);
		}
		return super.onKeyDown(keyCode, event);
	}
}